﻿// LILI128.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int lc[39], ld[89];
ofstream keyfile("LILIOUT_128.txt"); // 存储生成的key密钥流

// LFSRC 时钟控制子系统
int clockc()
{
	int gen = lc[0] ^ lc[4] ^ lc[6] ^ lc[8] ^ lc[22] ^ lc[24] ^ lc[25] ^ lc[37];
	for (int i = 38; i > 0; i--)
		lc[i] = lc[i - 1];
	lc[0] = gen;
	return 0;
}

// LFSRD 数据生成系统
int clockd(int clock)
{
	while (clock --)
	{
		int gen = ld[0] ^ ld[6] ^ ld[9] ^ ld[34] ^ ld[36] ^ ld[47] ^ ld[50] ^ ld[88];
		for (int i = 88; i > 0; i--)
			ld[i] = ld[i - 1];
		ld[0] = gen;
	}
	int key = ld[12] ^ ld[7] ^ ld[3] ^ ld[1] ^ ld[80] * ld[20] ^ ld[80] * ld[7] ^ ld[65] * ld[3] ^ ld[65] * ld[0] ^ ld[44] * ld[1] ^ ld[44] * ld[0] ^ ld[30] * ld[20] ^ ld[80] * ld[65] * ld[12] ^ ld[80] * ld[65] * ld[7] ^ ld[80] * ld[65] * ld[3] ^ ld[80] * ld[65] * ld[1] ^ ld[80] * ld[44] * ld[7] ^ ld[80] * ld[44] * ld[3] ^ ld[80] * ld[30] * ld[20] ^ ld[80] * ld[30] * ld[12] ^ ld[80] * ld[30] * ld[7] ^ ld[65] * ld[44] * ld[20] ^ ld[65] * ld[44] * ld[3] ^ ld[65] * ld[30] * ld[20] ^ ld[65] * ld[30] * ld[7] ^ ld[65] * ld[30] * ld[3] ^ ld[80] * ld[65] * ld[44] * ld[20] ^ ld[80] * ld[65] * ld[44] * ld[7] ^ ld[80] * ld[65] * ld[44] * ld[3] ^ ld[80] * ld[65] * ld[44] * ld[0] ^ ld[80] * ld[65] * ld[30] * ld[20] ^ ld[80] * ld[65] * ld[30] * ld[7] ^ ld[80] * ld[65] * ld[30] * ld[1] ^ ld[80] * ld[44] * ld[30] * ld[12] ^ ld[80] * ld[44] * ld[30] * ld[3] ^ ld[65] * ld[44] * ld[30] * ld[7] ^ ld[65] * ld[44] * ld[30] * ld[1] ^ ld[65] * ld[30] * ld[20] * ld[12] ^ ld[65] * ld[30] * ld[20] * ld[7] ^ ld[80] * ld[65] * ld[44] * ld[30] * ld[7] ^ ld[80] * ld[65] * ld[44] * ld[30] * ld[3] ^ ld[80] * ld[65] * ld[30] * ld[20] * ld[12] ^ ld[80] * ld[65] * ld[30] * ld[20] * ld[7] ^ ld[65] * ld[44] * ld[30] * ld[20] * ld[12] ^ ld[65] * ld[44] * ld[30] * ld[20] * ld[7] ^ ld[80] * ld[65] * ld[44] * ld[30] * ld[20] * ld[12] ^ ld[80] * ld[65] * ld[44] * ld[30] * ld[20] * ld[7];
	keyfile << key;
	return 0;
}

int main()
{
	fstream readInit("LILI_INPUT.txt"); // 从此文件中读取两个 LFSR寄存器的初始值
	for (int i = 0; i < 39; i++)
	{
		readInit >> lc[i];
	}
	for (int i = 0; i < 89; i++)
	{
		readInit >> ld[i];
	}
	readInit.close();
	long long test = 1000000;
	while (test --)
	{
		int clock = 2 * lc[11] + lc[19] + 1;
		clockd(clock);
		clockc();
	}
	keyfile.close();
}